#include "stdafx.h"
#include "InputService.h"
#include "InputDevice.h"

InputTask::InputTask(EventChannel* channel)
{
	m_KeyboardDevice = new KeyboardDevice(channel);
	m_MouseDevice = new MouseDevice(channel);
}

InputTask::~InputTask()
{
	SAFE_DELETE(m_KeyboardDevice);
	SAFE_DELETE(m_MouseDevice);
}

void InputTask::OnEvent( Event* evt )
{
	BEGIN_EVENT_HANDLER(evt);

	ON_EVENT(FrameUpdatedEvent)
	{
		UpdateInput(evt->DT);
	}

	END_EVENT_HANDLER;
}

void InputTask::UpdateInput(float dt)
{
	m_KeyboardDevice->UpdateKeyboard(dt);
	m_MouseDevice->UpdateMouse(dt);
	// Get Keyboard and Mouse information
}

//////////////////////////////////////////////////////////////////////////


InputService::InputService()
{
	m_InputChannel	= NULL;
	m_InputTask		= NULL;
}

InputService::~InputService()
{
}

void InputService::StartInputTask()
{
	if (!m_InputTask)
	{
		m_InputTask = new InputTask(m_InputChannel);
		m_InputTask->Start();
	}
}

void InputService::StopInputTask()
{
	if (m_InputTask)
	{
		m_InputTask->Stop();
		SAFE_DELETE(m_InputTask);
	}
}

void InputService::Hold()
{
	++m_HoldCount;
	if (m_HoldCount == 1)
	{
		
	}
}

void InputService::UnHold()
{
	--m_HoldCount;
}

void InputService::OnReset()
{
	m_InputChannel = g_EventService->CreateChannel();
	m_InputChannel->AddHandler(g_App->GetAppListener()->GetInputHandler());

	StopInputTask();
	m_HoldCount = 0;
}

//////////////////////////////////////////////////////////////////////////

bool InputService::KeyIsPressed(OIS::KeyCode code)
{
	return m_InputTask->m_KeyboardDevice->IsPressed(code);
}

bool InputService::KeyIsReleased( OIS::KeyCode code )
{
	return m_InputTask->m_KeyboardDevice->IsReleased(code);
}

bool InputService::KeyIsTriggered( OIS::KeyCode code )
{
	return m_InputTask->m_KeyboardDevice->IsTriggered(code);
}

bool InputService::KeyIsRepeated( OIS::KeyCode code )
{
	return m_InputTask->m_KeyboardDevice->IsRepeated(code);
}

bool InputService::KeyIsModifier( OIS::Keyboard::Modifier modifier )
{
	return m_InputTask->m_KeyboardDevice->IsModifier(modifier);
}

bool InputService::KeyIsCapsLock()
{
	return m_InputTask->m_KeyboardDevice->IsCapsLock();
}

//////////////////////////////////////////////////////////////////////////

bool InputService::MouseIsPressed(OIS::MouseButtonID code)
{
	return m_InputTask->m_MouseDevice->IsPressed(code);
}

bool InputService::MouseIsReleased( OIS::MouseButtonID code )
{
	return m_InputTask->m_MouseDevice->IsReleased(code);
}

bool InputService::MouseIsTriggered( OIS::MouseButtonID code )
{
	return m_InputTask->m_MouseDevice->IsTriggered(code);
}

bool InputService::MouseIsRepeated( OIS::MouseButtonID code )
{
	return m_InputTask->m_MouseDevice->IsRepeated(code);
}

